﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Web.App_Code;
using QXBook.Model;
using QXBook.Service;
using QXBook.Utils;
using System.Configuration;
using System.Web.UI.HtmlControls;

namespace Web.SysManage.Video
{
    public partial class VideoEdit : AdminBasePage
    {
        private void BindVideoType(HtmlSelect selectControl)
        {
            selectControl.Items.Clear();
            selectControl.Items.Add(new ListItem("请选择", "0"));
            selectControl.Items.Add(new ListItem(ModelEnums.GetEnumDescription(Part.JRQXP), CommonUtils.ToString((int)Part.JRQXP)));
            selectControl.Items.Add(new ListItem(ModelEnums.GetEnumDescription(Part.SYYX), CommonUtils.ToString((int)Part.SYYX)));
            selectControl.Items.Add(new ListItem(ModelEnums.GetEnumDescription(Part.RSWW), CommonUtils.ToString((int)Part.RSWW)));
            selectControl.Items.Add(new ListItem(ModelEnums.GetEnumDescription(Part.QXPGY), CommonUtils.ToString((int)Part.QXPGY)));
            selectControl.Items.Add(new ListItem(ModelEnums.GetEnumDescription(Part.SYGK), CommonUtils.ToString((int)Part.SYGK)));
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            lbtnSave.Attributes.Add("onmouseover", "this.className='tnew nbg'");
            lbtnSave.Attributes.Add("onmouseout", "this.className='tnew'");
            if (!IsPostBack)
            {
                BindVideoType(selVideoType);
                if (!string.IsNullOrEmpty(CommonUtils.Get("VideoID")))
                {
                    Title = "修改视频信息";
                    hVideoID.Value = CommonUtils.Get("VideoID");
                    VideoInfo video = ServiceFactory.CreateInstance<VideoService>().GetModel(CommonUtils.ToInt(hVideoID.Value));
                    this.DataBind(video);
                }
                else
                {
                    Title = "添加视频";
                }
            }
            lbtnSave.Click += new EventHandler(btnSave_Click);
            btnSave.Click += new EventHandler(btnSave_Click);
        }

        void btnSave_Click(object sender, EventArgs e)
        {
            Save();
        }

        private void DataBind(IModelBase model)
        {
            VideoInfo video = model as VideoInfo;
            iTitle.Value = video.Title;
            selVideoType.Value = ((int)video.VideoType).ToString();
            if (video.IsNeedScore == YesNo.Yes) { this.rNeedScoreY.Checked = true; } else { this.rNeedScoreN.Checked = true; }
            iAuthor.Value = video.Author;
            iScore.Value = video.Score.ToString();
            taContent.Value = video.Content;
        }

        private VideoInfo GetModel(ref VideoInfo model)
        {
            string title = CommonUtils.Post("iTitle");
            string author = CommonUtils.Post("iAuthor");
            int videoType = CommonUtils.ToInt(CommonUtils.Post("selVideoType"));
            YesNo isNeedScore = (YesNo)CommonUtils.ToInt(CommonUtils.Post("rNeedScore"));
            float score = CommonUtils.ToFloat(CommonUtils.Post("iScore"));
            string smallimg = string.Empty;
            string bigimg = string.Empty;
            HttpFileCollection files = Request.Files;
            if (files.Count > 0)
            {
                HttpPostedFile file = files[0];
                string normalPath = ConfigurationManager.AppSettings["ImgNormal"];   //普通图片路径
                string smallPath = ConfigurationManager.AppSettings["ImgSmall"];        //缩略图片路径
                FileUploadHelper fileUpload = new FileUploadHelper();
                fileUpload.PostedFile = file;
                fileUpload.SetFile(); //设置文件信息
                //保存缩略图到small中
                fileUpload.FileCompressPath = smallPath;
                fileUpload.CompressImage(218, 203, "Cut");
                //保存图片到normal中
                fileUpload.FilePath = normalPath;
                fileUpload.SaveFile();
                smallimg = fileUpload.RefSmallFilePath;
                bigimg = fileUpload.RefSmallFilePath;
            }
            string content = CommonUtils.Post("taContent");

            if (string.IsNullOrEmpty(title))
            {
                base.ResponseErch("标题不能为空!");
                return null;
            }

            model.Title = title;
            model.VideoType = (VideoType)videoType;
            model.Author = author;
            model.IsNeedScore = isNeedScore;
            model.Score = score;
            model.SmallImg = smallimg;
            model.BigImg = bigimg;
            model.Content = content;
            return model;
        }

        private void Save()
        {
            string rtmessage = string.Empty;
            string resultMsg = string.Empty;
            try
            {
                VideoInfo video = string.IsNullOrEmpty(hVideoID.Value) ? new VideoInfo() : ServiceFactory.CreateInstance<VideoService>().GetModel(CommonUtils.ToInt(hVideoID.Value));
                video = GetModel(ref video);

                if (!string.IsNullOrEmpty(hVideoID.Value))
                {
                    rtmessage = ServiceFactory.CreateInstance<VideoService>().UpdateVideo(base.adminInfo.AdminName, video);
                    resultMsg = "管理员信息修改成功!";
                }
                else
                {
                    rtmessage = ServiceFactory.CreateInstance<VideoService>().AddVideo(base.adminInfo.AdminName, video);
                    resultMsg = "成功添加新的管理员!";
                }
            }
            catch (Exception ex)
            {
                base.AjaxErch("{state:false,message:\"" + CommonUtils.JSEncode(ex.Message.ToString()) + "\"}");
                return;
            }

            base.ResponseErch(rtmessage, resultMsg);
        }
    }
}